<?php

require_once 'Controller.php';

class UserController extends Controller {
    
    public function usersAction(){
        $this->_autoRender = false;
        $this->_view->layout->title = 'Users Page';
        try {
            $this->_view->users = $this->_model->all($this->_request->getQuery());
        } catch (Exception $ex) {
            return $this->_response->fileNotFound($ex->getMessage());
        }
        $this->_view->scol = $this->_request->getQuery('sc', 'id');
        $this->_view->sord = $this->_request->getQuery('so', 'asc');
        $this->_view->fcol = $this->_request->getQuery('fc', 'pib');
        $this->_view->fval = $this->_request->getQuery('fv', '');
        $this->_response->render('user/users');
    }
    
    public function addAction(){
        $this->_view->layout->title = 'Add New User';
        $new = null;
        if ( $this->_request->isPost() ){
            $gid = $this->_request->getPost('gid', null);
            $pib = $this->_request->getPost('pib', null);
            $email = $this->_request->getPost('email', null);
            $phone = $this->_request->getPost('phone', null);
            $pass = $this->_request->getPost('pass', null);
            $verify = $this->_request->getPost('verify', null);
            
            $new = array( 'gid' => $gid, 'pib' => $pib, 'email' => $email, 'phone' => $phone, 'pass' => $pass, 'verify' => $verify, );
            
            $this->_validator->notBlank('PIB', $pib);
            $this->_validator->notBlank('Email', $email);
            $this->_validator->email('email', $email);
            $this->_validator->notBlank('Phone', $phone);
            $this->_validator->notBlank('Password', $pass);
            $this->_validator->notBlank('Confirm', $verify);
            if ($verify !== $pass){
                $this->_validator->addError('Password', 'Must be equal.');
            }
            if ( $this->_validator->isValid() ){
                try{
                    $this->_model->add($gid, $pib, $email, $phone, $pass);
                    $this->_response->redirect('/index.php/user/users');
                } catch (Exception $ex){
                    $this->_validator->addError('SQL ERROR', $ex->getMessage());
                }
            }
        }
        $this->_view->new = $new;
        $this->_view->groups = $this->_model->groups();
        $this->_view->errors = $this->_validator->getErrors();
    }
    
    public function editAction(){
        $this->_view->layout->title = 'Edit User';
        $id = $this->_request->getQuery('id', null);
        $user = $this->_model->findById($id);
        if ( ! $user ){
            return $this->_response->fileNotFound();
        }
        $edit = null;
        if ( $this->_request->isPost() ){
            $gid = $this->_request->getPost('gid', null);
            $pib = $this->_request->getPost('pib', null);
            $email = $this->_request->getPost('email', null);
            $phone = $this->_request->getPost('phone', null);
            $pass = $this->_request->getPost('pass', null);
            $verify = $this->_request->getPost('verify', null);
            
            $edit = array('gid'=>$gid, 'pib'=>$pib, 'email'=>$email, 'phone'=>$phone, 'pass'=>$pass, 'verify'=>$verify);
            
            $this->_validator->notBlank('PIB', $pib);
            $this->_validator->notBlank('Email', $email);
            $this->_validator->email('Email', $email);
            $this->_validator->notBlank('Phone', $phone);
            $this->_validator->notBlank('Password', $pass);
            $this->_validator->notBlank('Confirm', $verify);
            if ($verify !== $pass){
                $this->_validator->addError('Password', 'Must be equal.');
            }
            if ( $this->_validator->isValid() ){
                try{
                    $this->_model->edit($id, $gid, $pib, $email, $phone, $pass);
                    $this->_response->redirect('/index.php/user/users');
                } catch (Exception $ex){
                    $this->_validator->addError('SQL ERROR', $ex->getMessage());
                }
            }
        }
        $this->_view->edit = $edit;
        $this->_view->groups = $this->_model->groups();
        $this->_view->user = $user;
        $this->_view->errors = $this->_validator->getErrors();
    }
    
    public function delAction(){
        $this->_autoRender = false;
        if ( ! $this->_request->isPost() ){
            return $this->_response->fileNotFound();
        }
        $id = $this->_request->getPost('id', null);
        try {
            $this->_model->del($id);
        } catch (Exception $ex) {
            
        }
        $this->_response->redirect('/index.php/user/users');
    }
    
}
